package ext.tianma.change.processors;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import wt.change2.WTChangeOrder2;
import wt.fc.WTObject;
import wt.part.WTPart;
import wt.util.WTException;

import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormResult;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;

import ext.com.core.CoreUtil;

public class ModifiedFinishedGoodTableProcessors extends DefaultObjectFormProcessor{

	@Override
	public FormResult doOperation(NmCommandBean nmcommandbean, List list)
			throws WTException {
		FormResult formResult = super.doOperation(nmcommandbean, list);
		
		NmOid nmoid = nmcommandbean.getActionOid(); 
		WTObject contextObj = (WTObject) nmoid.getWtRef().getObject();
		WTChangeOrder2 ecn = null;
		if( ( contextObj != null ) && ( contextObj instanceof WTChangeOrder2 ) ){
			ecn = ( WTChangeOrder2 )contextObj;
		}
		
		Map<String, String[]> map = nmcommandbean.getRequest().getParameterMap();
		
		Set<String> set = map.keySet();
		Iterator<String>  iterator = set.iterator();
		while( ( iterator != null ) && ( iterator.hasNext() ) ){
			String key = iterator.next();
			if( ( key != null ) && ( key.equals( "pjl_selPJLsa1__1" ) )){
				
				String[]  mapValue = map.get(key);
				if( ( mapValue != null ) && ( mapValue.length>0 ) ){
					
					for (int i = 0; i < mapValue.length; i++) {
						
						String value = mapValue[i];
						value =ModifiedFinishedGoodUtil. getValue( value );	
						if( ( value != null ) && ( !value.equals( "" ) ) && ( ecn != null ) ){
							updateIBAFinishedGood( ecn , value );
						}
					}
					
				}
				
			}
		}				
		return formResult;
	}
	
	/**
	 * 往IBA上面写数据
	 * @param oid
	 * @throws WTException 
	 */
	private static void updateIBAFinishedGood( WTChangeOrder2 ecn , String oid ) throws WTException{		
		WTObject wtObject = CoreUtil.getWTObjectByOid( oid );
		String value = "";
		
		if( ( wtObject != null ) && ( wtObject instanceof WTPart ) ){
			
			WTPart part = ( WTPart )wtObject;
			value = ModifiedFinishedGoodUtil.getPartInfo( part );
			if( value != null ){
				ModifiedFinishedGoodUtil.updateIBAValue( ecn , value );
			}
			
		}
		
	}
	
	
	
	
}
